// pages/chat/chat.js
var util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        pageNum:1,
        readMoreFlag:false,
        list:[],
        value:"",
        adminid:"",
        userImg:"",//聊天用户头像
        userInfo:""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var system = wx.getSystemInfoSync();
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        this.setData({
            height: system.screenHeight-130,
            adminid: options.adminid,
            userImg: options.userImg,
            userInfo: userInfo
        });
        this.getList();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    //发送聊天信息
    sendMsg:function(){
        wx.showLoading({
            title: '发送中...',
        })
        var that = this;
        var fromAdminid = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var toAdminid = this.data.adminid;
        var content = this.data.value;
        util.wxAjax("glMessage/saveMessage","post",{
            fromAdminid: fromAdminid,
            toAdminid: toAdminid,
            msg:content
        },function(res){
            wx.hideLoading();
            var data = res.data;
            that.setData({
                value:"",
                pageNum:1
            });
            if (data.data == 200 || data.result == "200"){
                that.getList(true);
            }else{
                wx.showModal({
                    title: '提示信息',
                    content: data.msg,
                })
            }
        });
    },
    setValue:function(options){
        var value = options.detail.value;
        this.setData({
            value:value
        });
    },
    //加载消息
    getList:function(clear){
        var that = this;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var fromAdminid = userInfo.smAdmin.id;
        var toAdminid = this.data.adminid;
        var pageNum = this.data.pageNum;
        if(!clear){
            var list = this.data.list;
        }else{
            var list = [];
        }
        util.wxAjax("glMessage/myMessageList","post",{
            fromAdminid: fromAdminid,
            toAdminid: toAdminid,
            pageNum: pageNum
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                if(!!data.data){
                    var list11 = data.data;
                    for (let i = 0, len = list11.length; i < len; i++) {
                        list11[i].createDate = util.format(list11[i].createDate);
                        list.unshift(list11[i]);
                    }
                    that.setData({
                        list: list,
                        readMoreFlag: (list11.length > 19 ? true : false)
                    });
                    that.getBottom();
                }
            }
        });
    },
    //上拉刷新
    onPullDownRefresh: function () {
        var flag = this.data.readMoreFlag;
        // 显示顶部刷新图标  
        var that = this;
        if (flag) {
            wx.showNavigationBarLoading();
            var pageNum = this.data.pageNum;
            this.setData({
                pageNum: ++pageNum
            });
            that.getList();
        }else{
            wx.stopPullDownRefresh();
        }
    },
    //滚动到底部
    getBottom:function(){
        wx.createSelectorQuery().select('.chat_content').boundingClientRect(function (rect) {
            console.log(rect);
            // 使页面滚动到底部
            wx.pageScrollTo({
                scrollTop: rect.height
            })
        }).exec();
    }
})